
package com.j2eefast.bussiness.contract.controller;

import com.j2eefast.bussiness.contract.entity.BusClassifyEntity;
import com.j2eefast.bussiness.contract.entity.BusInstallsEntity;
import com.j2eefast.bussiness.contract.service.BusClassifyService;
import com.j2eefast.bussiness.contract.service.BusInstallsService;
import com.j2eefast.bussiness.contract.service.BusMeteringUnitService;
import com.j2eefast.common.core.base.entity.Ztree;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>设备分类</p>
 *
 * @author: summer
 * @date: 2021-06-01 10:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bus/classify")
public class BusClassifyController extends BaseController {

	private String urlPrefix = "modules/bus/classify";

	@Autowired
	private BusClassifyService busClassifyService;
	@Autowired
	private SysUserService sysUserService;

	@RequiresPermissions("bus:classify:view")
	@GetMapping()
	public String process() {
		return urlPrefix + "/index";
	}

	/**
	 * 页面获取公司信息列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("bus:classify:list")
	@ResponseBody
	public List<BusClassifyEntity> list(@RequestParam Map<String, Object> params) {
		return  busClassifyService.findList(params);
	}

	/**
	 * 加载公司列表树
	 */
	@GetMapping("/treeData")
	@RequiresPermissions("bus:classify:list")
	@ResponseBody
	public List<Ztree> treeData(Long parentId){
		List<Ztree> ztrees = busClassifyService.findCompTree(parentId);
		return ztrees;
	}



	/**
	 * 新增设备分类
	 */
	@GetMapping("/add/{id}")
	public String add(@PathVariable("id") Long id, ModelMap mmap){

		//mmap.put("comp",  busClassifyService.findCompById(id));
		BusClassifyEntity busClassifyEntity = busClassifyService.findCompById(id);
		mmap.put("classify",  busClassifyEntity);
		return urlPrefix + "/add";
	}

	/**
	 * 保存配置
	 */
	@BussinessLog(title = "设备分类", businessType = BusinessType.INSERT)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@RequiresPermissions("bus:classify:add")
	@ResponseBody
	public ResponseData save(@Validated BusClassifyEntity comp) {
//		busClassifyService.checkDataScope(comp.getParentId());
		ValidatorUtil.validateEntity(comp);
//		if (!busClassifyService.checkCompNameUnique(comp)){
//			return error("设备分类'" + comp.getClassName() + "'失败,名称已存在");
//		}

		busClassifyService.add(comp);
		return success();
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap){
		BusClassifyEntity busClassifyEntity = busClassifyService.findCompById(id);
		System.out.println("1111111111111");
		System.out.println(busClassifyEntity.getParentName());
		System.out.println("1111111111111");
		mmap.put("classify",  busClassifyEntity);
		//mmap.put("areaNames",  busClassifyService.getAreaNames(busClassifyEntity.getAreaIds()));
		return urlPrefix + "/edit";
	}

	/**
	 * 修改公司
	 */
	@BussinessLog(title = "设备分类", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@RequiresPermissions("bus:classify:edit")
	@ResponseBody
	public ResponseData update(@Validated BusClassifyEntity comp) {
		//check data scope
//		busClassifyService.checkDataScope(comp.getId());
		System.out.println("2222222");
		ValidatorUtil.validateEntity(comp);
//		if (!busClassifyService.checkCompNameUnique(comp)) {
//			return error("修改公司'" + comp.getName() + "'失败,名称已存在");
//		}
		System.out.println("2222222");

		System.out.println("22222222");
		if(busClassifyService.update(comp)){
			return success();
		}else{
			return error("修改失败!");
		}
	}

//	/**
//	 * 删除公司
//	 */
//	@BussinessLog(title = "公司管理", businessType = BusinessType.DELETE)
//	@RequestMapping("/del/{id}")
//	@RequiresPermissions("bus:classify:del")
//	@ResponseBody
//	public ResponseData delete(@PathVariable("id") Long id) {
//
//		//busClassifyService.checkDataScope(compId);
//		System.out.println("11111");
//			System.out.println(id);
//		System.out.println("1111");
//			if(busClassifyService.delSysCompById(id)) {
//				return success();
//			}else {
//				return error("删除失败!");
//			}
//	}

	/**
	 * 设备分类删除
	 */
	@RepeatSubmit
	@RequiresPermissions("pms:classify:del")
	@BussinessLog(title = "删除", businessType = BusinessType.UPDATE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData doDelete(HttpServletRequest request) {
		Long  ids=Long.parseLong(request.getParameter("id")) ;
		return busClassifyService.delSysCompById(ids) ? success() : error("删除失败!");
	}


}
